package ArrayList.DBLink;

import java.sql.*;

public class AddressList {
    public static Connection connectDB(String DBName, String usr, String pwd) {
        Connection con = null;
        String url = "jdbc:mysql://localhost:3306/" + DBName
                + "?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception e) {
            System.out.println("数据库连接失败");
        }
        try {
            con = DriverManager.getConnection(url, usr, pwd);
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }
        return con;
    }
    public static ResultSet query(Connection con) throws SQLException {
        Statement sql = con.createStatement();
        String s = "select * from contacts";
        return sql.executeQuery(s);
    }
    public static ResultSet query_add(Connection con) throws SQLException {
        Statement sql = con.createStatement();
        String s = "select MAX(`id`) from contacts";
        return sql.executeQuery(s);
    }
    public static void add(Connection con,int point,String name,String phone,String address) throws SQLException {
        Statement sql = con.createStatement();
        String s = "insert into contacts values('" + point + "','" + name + "','"
                + phone + "','" + address + "')";
        sql.executeUpdate(s);
    }
    public static ResultSet query_delete(Connection con,int num1) throws SQLException {
        Statement sql = con.createStatement();
        String s = "select * from contacts where id='" + num1 + "'";
        return sql.executeQuery(s);
    }
    public static int delete(Connection con,int num) throws SQLException {
        Statement sql = con.createStatement();
        String s = "delete from contacts where id='" + num + "'";
        @SuppressWarnings("unused")
        int a = sql.executeUpdate(s);
        return a;
    }
    public static ResultSet query_update(Connection con,int num2) throws SQLException {
        Statement sql = con.createStatement();
        String s = "select * from contacts where id='" + num2 + "'";
        return sql.executeQuery(s);
    }
    public static void update(Connection con,int num2,String name,String phone,String address,int num3) throws SQLException {
        Statement sql = con.createStatement();
        String sql2 = "update contacts set `id`='" + num2 + "',`name`='"
                + name + "',`phone`='" + phone + "',`address`='"
                + address + "' where id='" + num3 + "'";
        sql.executeUpdate(sql2);
    }
}
